<template>
	<div v-show="duty_id!=''">



		<van-button type="primary" @click="popupShow_">关联库房</van-button>
		<van-button type="info" @click="popupShow">关联产品分类</van-button>

		<van-popup v-model="show" position="bottom" :style="{ height: '60%' }">

			<div class="pop-title">职务关联类别</div>
			<van-cell-group v-for="item in all_ancestor">
				<van-cell @click="chaneg_choice(item)" :title="item.class_name"
					:class="[now_duty_class_list_name.indexOf(item.class_name) == -1?'':'color_red']"
					:value="now_duty_class_list_name.indexOf(item.class_name) == -1?'未关联': '关联'" />
			</van-cell-group>
			</van-checkbox-group>

			<div style="height: 120px;"></div>

		</van-popup>


		<van-popup v-model="show_" position="bottom" :style="{ height: '60%' }">

			<div class="pop-title">职务关联库房</div>
			<van-cell-group v-for="item in all_warehouse">
				<van-cell :class="[now_duty_warehouse_list_name.indexOf(item.warehouse_name) == -1?'':'color_red']"
					:label="laber_str(item)" @click="chaneg_choice_(item)" :title="item.warehouse_name"
					:value="now_duty_warehouse_list_name.indexOf(item.warehouse_name) == -1?'未关联': '关联'" />
			</van-cell-group>
			</van-checkbox-group>

			<div style="height: 120px;"></div>


			<!-- 库房权限选择 -->

			<van-popup v-model="show__" position="bottom" :style="{ height: '60%' }">

				<div class="pop-title">选择权限的库房：{{warehouse_name}}</div>


				<van-cell-group>
					<van-cell title="入库权限">
						<template #right-icon>
							<van-checkbox v-model="in_"></van-checkbox>
						</template>
					</van-cell>

					<van-cell title="出库权限">
						<template #right-icon>
							<van-checkbox v-model="out"></van-checkbox>
						</template>
					</van-cell>

					<van-cell title="入库单查询权限">
						<template #right-icon>
							<van-checkbox v-model="in_odd"></van-checkbox>
						</template>
					</van-cell>

					<van-cell title="出库单查询权限">
						<template #right-icon>
							<van-checkbox v-model="out_odd"></van-checkbox>
						</template>
					</van-cell>
					
					<van-cell title="入库单操作权限">
						<template #right-icon>
							<van-checkbox v-model="in_operation"></van-checkbox>
						</template>
					</van-cell>
					
					<van-cell title="出库单操作权限">
						<template #right-icon>
							<van-checkbox v-model="out_operation"></van-checkbox>
						</template>
					</van-cell>

					<van-cell title="审核权限">
						<template #right-icon>
							<van-checkbox v-model="examine"></van-checkbox>
						</template>
					</van-cell>

				</van-cell-group>

				<div style="margin-top: 10px;">
					<van-button type="danger" style="width: 50%; margin-top: 5px;" @click="del_duty_warehouse_ship_">解除职务和当前库房关系</van-button>
					<van-button type="primary" style="width: 50%; margin-top: 5px;" @click="add_duty_warehouse_ship_">提交权限修改</van-button>
					<!-- <van-button type="primary" style="width: 50%; margin-top: 5px;">提交权限修改</van-button> -->
				</div>
				<div style="height: 120px;"></div>
			</van-popup>
		</van-popup>





	</div>
</template>

<script>
	import {
		Toast,
		Notify,
		Dialog
	} from 'vant'
	
	import {
		select_all_class_ship,
		del_duty_class_ship,
		add_duty_class_ship
	} from "../../utils/duty_class_ship.js"

	import {
		select_all_duty_warehouse_ship,
		del_duty_warehouse_ship,
		add_duty_warehouse_ship,
		update_duty_warehouse_ship
	} from "../../utils/duty_warehouse_ship.js"

	import {selectAllAncestor_} from "../../utils/select_all_ancestor.js"
	import {get_all_warehouse_} from '../../utils/get_all_warehouse.js'
	export default {
		name: '',
		components: {},
		props: ['dutyId'],
		data() {
			return {
				show: false,
				show_: false,
				show__: false,
				duty_id: '',
				all_warehouse: [],
				all_ancestor: [],

				now_duty_class_list: [],
				now_duty_class_list_name: [],

				now_duty_warehouse_list: [],
				now_duty_warehouse_list_name: [],

				in_: false,
				out: false,
				in_odd: false,
				out_odd: false,
				examine: false,
				out_operation:false,
				in_operation:false,
				warehouse_name: '',
				warehouse_id: '',


			}
		},
		methods: {

			chaneg_choice(item) {

				// console.log(item)
				let message = ''
				if (this.now_duty_class_list_name.indexOf(item.class_name) == -1) {

					message = '添加类别：' + item.class_name
				} else {
					message = '解除类别：' + item.class_name
				}


				Dialog.confirm({
						title: message,
						message: message
					})
					.then(() => {
						// on confirm

						if (message.indexOf('添加类别') != -1) {

							add_duty_class_ship(
								this.duty_id, item.id,

								(res) => {
									// console.log(res)
									Notify({
										type: 'success',
										message: '添加成功'
									})

								}
							)

						} else if (message.indexOf('解除类别') != -1) {
							console.log('xxxx')
							del_duty_class_ship(
								this.duty_id, item.id,

								(res) => {
									Notify({
										type: 'success',
										message: '解除成功'
									})
								}
							)
						}

						this.select_all_class_ship_()
					})
					.catch(() => {
						// on cancel
					});

			},


			select_all_class_ship_() {

				this.now_duty_class_list = []
				this.now_duty_class_list_name = []

				select_all_class_ship(

					(res) => {
						// console.log(res)
						let result = []
						let result_name = []

						for (let i = 0; i < res.length; i++) {
							if (res[i].duty_id == this.duty_id) {
								result.push(res[i])
								result_name.push(res[i].class_name)
								

							}
						}

						this.now_duty_class_list = result
						this.now_duty_class_list_name = result_name


					}

				)

			},

			//关联类
			popupShow() {

				this.select_all_class_ship_()
				// console.log(this.now_duty_class_list)
				this.show = true
			},

			popupShow_() {

				// this.select_all_class_ship_()
				// console.log(this.now_duty_class_list)
				this.select_all_duty_warehouse_ship_()
				this.show_ = true
			},

			select_all_duty_warehouse_ship_() {

				select_all_duty_warehouse_ship(
					(res) => {

						let result = []
						let result_name = []

						// console.log(res)
						for (let i = 0; i < res.length; i++) {
							if (res[i].duty_id == this.duty_id) {
								result.push(res[i])
								result_name.push(res[i].warehouse_name)

							}
						}

						this.now_duty_warehouse_list = result
						this.now_duty_warehouse_list_name = result_name


					}
				)

			},

			laber_str(item) {
				let index = this.now_duty_warehouse_list_name.indexOf(item.warehouse_name)
				if (index != -1) {
					let val = this.now_duty_warehouse_list[index]
					// console.log(val)
					let str_ = ''
					let str = str_.concat('入库权限：', val.in_ ? '开' : '关', '  |  ',
						'出库权限：', val.out ? '开' : '关', '  |  ',
						'出库权限：', val.out ? '开' : '关', '  |  ',
						'出库权限：', val.out ? '开' : '关', '  |  ',
						'入库单操作权限：', val.in_operation ? '开' : '关', '  |  ',
						'出库单操作权限：', val.out_operation ? '开' : '关', '  |  ',
						'审核权限：', val.examine ? '开' : '关'
					)

					

					return str
				}
			},

			chaneg_choice_(item) {
				
				this.in_= false
				this.out = false
				this.in_odd = false
				this.out_odd = false
				this.out_operation = false
				this.in_operation = false
				this.examine = false

				let index = this.now_duty_warehouse_list_name.indexOf(item.warehouse_name)
				// console.log(index)
				if (index != -1) {
					let val = this.now_duty_warehouse_list[index]
				
					this.in_= val.in_
					this.out = val.out
					this.in_odd = val.in_odd
					this.out_odd = val.out_odd
					this.out_operation = val.out_operation
					this.in_operation = val.in_operation
					this.examine = val.examine
					
				}
				this.warehouse_name = item.warehouse_name
				this.warehouse_id = item.id
				this.show__ = true
			},
			
			del_duty_warehouse_ship_(){
				
				del_duty_warehouse_ship(
					this.duty_id, this.warehouse_id,
					(res)=>{
						console.log(res)
						Notify({'message':'解除成功', type:'success'})
						this.select_all_duty_warehouse_ship_()
						this.show__ = false
					}
					
					
				)
				
			},
			
			add_duty_warehouse_ship_(){
				add_duty_warehouse_ship(
					this.duty_id, this.warehouse_id,
					
					(res)=>{
						update_duty_warehouse_ship(
							this.duty_id, this.warehouse_id,
							this.out, this.in_ , this.out_odd, this.in_odd, 
							this.out_operation, this.in_operation,
							this.examine,
							(res)=>{
								// console.log(res)
								Notify({'message':'更新成功', type:'success'})
								this.select_all_duty_warehouse_ship_()
								this.show__ = false
							}
						)
					}
					
				)
			}

		},
		computed: {},
		watch: {
			dutyId: function(val, oldVal) {
				// console.log(val)
				// console.log('new: %s, old: %s', val, oldVal)
				this.duty_id = val

			},
			result: function(val, oldVal) {
				// console.log(val)
				// console.log('new: %s, old: %s', val, oldVal)
				// console.log(val)

			}
		},
		created() {},
		mounted() {

			//生成items数据
			get_all_warehouse_(
				(res) => {
					// console.log(res)

					this.all_warehouse = res

				}
			)

			selectAllAncestor_(
				(res) => {
					// console.log(res)

					this.all_ancestor = res

				}
			)

		},
	}
</script>

<style scoped lang="less">
	.van-button {
		width: 50%;
	}

	.pop-title {
		font-weight: 800;
		margin: 5px;
		width: 100%;
		text-align: center;
	}

	.color_red {
		color: red;
	}
</style>
